package com.iss.card.view.overrideview;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

public class ParticipatorSortLetterListView extends View {
	private static final String TAG = ParticipatorSortLetterListView.class.getSimpleName();

	private int mChoose = -1;

	private Paint mPaint;

	private boolean isShowBg = false;

	private int letterSize = 0;

	int singleHeight = 0;
	private String[] mIndexKeySets = null;
	
	private OnTouchingLetterChangedListener mChangedListener;

	public ParticipatorSortLetterListView(Context context) {
		super(context);
		mPaint = new Paint();
	}

	public ParticipatorSortLetterListView(Context context, AttributeSet attrs) {
		super(context, attrs);
		mPaint = new Paint();
	}

	public ParticipatorSortLetterListView(Context context, AttributeSet attrs,
			int defStyle) {
		super(context, attrs, defStyle);
		mPaint = new Paint();
	}

	public void setIndexKeysets(String[] datas) {
		mIndexKeySets = datas;
		letterSize = mIndexKeySets.length;
	}
	
	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		drawLetter(canvas);
	}

	@Override
	public boolean dispatchTouchEvent(MotionEvent event) {
		return super.dispatchTouchEvent(event);
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		if (mIndexKeySets == null) {
			return false;
		}
		
		doClickEvent(event);
		return true;
	}

	public String[] getIndexSets() {
		return mIndexKeySets;
	}
	
	public void setOnTouchingLetterChangedListener(
			OnTouchingLetterChangedListener listener) {
		this.mChangedListener = listener;
	}

	public interface OnTouchingLetterChangedListener {
		public void onTouchingLetterChanged(String s);
	}

	/*
	 * �¼���� 
	 */
	private void doClickEvent(MotionEvent event) {
		final int action = event.getAction();
		final float y = event.getY();
		final int oldChoose = mChoose;
		final OnTouchingLetterChangedListener listener = mChangedListener;
		
		int c = (int) (y / singleHeight);  
        if (c >= letterSize) {  
            c = letterSize - 1;  
        } else if (c < 0) {  
            c = 0;  
        }  
		switch (action) {
		case MotionEvent.ACTION_DOWN:
			isShowBg = true;
			if (oldChoose != c && listener != null) {
				if (c > -1 && c < letterSize) {
					listener.onTouchingLetterChanged(mIndexKeySets[c]);
					mChoose = c;
					invalidate();
				}
			}

			break;
		case MotionEvent.ACTION_MOVE:
			if (oldChoose != c && listener != null) {
				if (c > -1 && c < letterSize) {
					listener.onTouchingLetterChanged(mIndexKeySets[c]);
					mChoose = c;
					invalidate();
				}
			}
			break;
		case MotionEvent.ACTION_UP:
			isShowBg = false;
			mChoose = -1;
			invalidate();
			break;
		}
	}

	/*
	 * ����ĸ
	 */
	private void drawLetter(Canvas canvas) {
		if (mIndexKeySets == null) {
			return;
		}
		
		if (isShowBg) {
			canvas.drawColor(Color.parseColor("#40000000"));
		}

		
		int height = getHeight();
		int width = getWidth();
		singleHeight = height / letterSize;
		for (int index = 0; index < letterSize; index++) {
			mPaint.setColor(Color.WHITE);
			mPaint.setTypeface(Typeface.DEFAULT_BOLD);
			mPaint.setAntiAlias(true);
			if (index == mChoose) {
				mPaint.setColor(Color.parseColor("#3399ff"));
				mPaint.setFakeBoldText(true);
			}
			String value = mIndexKeySets[index];
			float xPos = width / 2 - mPaint.measureText(value) / 2;
			float yPos = singleHeight * index + singleHeight;
			canvas.drawText(value, xPos, yPos, mPaint);
			mPaint.reset();
		}
	}

}
